package jee.boot.module.mail.queue;

import jee.boot.common.utils.JsonMapper;
import jee.boot.entity.mail.Mail;
import org.apache.activemq.command.ActiveMQQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Queue;

/**
 * 邮件生产
 */
@Service
public class MailProducer {
	private static final Logger log=LoggerFactory.getLogger(MailProducer.class);
	@Autowired
	private JmsMessagingTemplate jmsTemplate;
	@Value("${spring.mail.channel:mail.queue}")
	private String channel;
	private Queue queue=null;
	/**
	 * 发送邮件到队列中
	 * @param mail 邮件任务对象
	 */
	public void send(Mail mail){
		String jsonString = JsonMapper.toJsonString(mail);
		this.sendMessage(jsonString);
	}
	/**
	 * 发送邮件到队列中
	 * @param message 邮件任务json
	 */
	public void sendMessage(final String message) {
		if(queue==null){
			queue= new ActiveMQQueue(channel);// 注册邮件消息队列
		}
		log.debug("mq:send["+channel+"]"+message);
		jmsTemplate.convertAndSend(queue, message);
	}
}
